<template>
  <el-dialog
    v-el-drag-dialog
    :title="title"
    :visible.sync="visible"
    width="700px"
  >
    <lb-table
      ref="lbTable"
      border
      element-loading-text="加载中"
      :column="tableData.column"
      :data="tableData.data"
      :page-size="10"
      pagination
      :total="tableData.total"
      :immediate-fetch="false"
      @fetch="fetchData"
    />
    <scan-detail ref="detail" />
  </el-dialog>
</template>

<script>
  import { queryScanStatDetailList } from '@/api'
  import ScanDetail from './ScanDetail'
  import moment from 'dayjs'

  export default {
    name: 'SignDetail',
    components: {
      ScanDetail
    },
    data() {
      return {
        visible: false,
        title: '',
        billType: '',
        temp: {},
        query: {},
        date: [],
        params: {},
        tableData: {
          total: 0,
          column: [
            {
              prop: 'operator',
              label: '人员'
            },
            {
              prop: 'goodsCount',
              label: '数量'
            },
            {
              label: '扫码详情',
              render: (h, { row }) => <el-link underline={false} onClick={() => this.openScanDetail(row)}>查看</el-link>
            }
          ],
          data: []
        }
      }
    },
    methods: {
      open(temp, title, date, billType, query) {
        this.title = title
        this.billType = billType
        this.query = query
        return new Promise(resolve => {
          this.temp = temp
          this.date = date
          this.resolve = resolve
          this.visible = true
          this.fetchData({ currentPage: 1, pageSize: 10 })
        })
      },
      async fetchData({ currentPage, pageSize }) {
        const params = {
          billType: this.billType,
          goodsId: this.temp.goodsId,
          gdStatus: this.temp.gdStatus,
          inStoreId: this.query.inStoreId,
          outStoreId: this.query.inStoreId,
          currentPage,
          pageSize
        }
        if (this.date && this.date.length > 0) {
          params.operatorTime = moment(this.date[0]).format()
          params.operatorTimeEnd = moment(this.date[1]).format()
        }
        this.params = params
        const res = await queryScanStatDetailList(params)
        this.tableData.data = res.data.rows
        if (currentPage === 1) this.tableData.total = res.data.total
      },
      async openScanDetail(row) {
        await this.$refs.detail.open(row, this.params)
      }
    }
  }
</script>

<style scoped>

</style>
